import asyncio as a


async def task(s, char, n, sleep):
    await s.acquire()
    for i in range(n):
        if sleep:
            await a.sleep(sleep)
        print(char, end='', flush=True)
    print(f'"{char}" over.')
    s.release()

if '__main__' == __name__:
    
    async def main():
        N, M, sleep = 2, 4, 0.001
        s = a.Semaphore(N)
        tasks = []
        for m in range(M):
            # await a.sleep(0.2)
            tasks.append(a.create_task(task(s, str(m), 1000, sleep)))
        for t in tasks:
            await t
            
    a.run(main())
    